package com.thermofisher.dsc.amanda.common.excel.scheduled;

import com.alibaba.excel.ExcelReader;
import com.alibaba.excel.metadata.Sheet;
import com.thermofisher.dsc.amanda.common.excel.listener.ExcelListener;
import com.thermofisher.dsc.amanda.common.excel.mapper.ExcelCacheMapper;
import com.thermofisher.dsc.amanda.common.excel.model.SkuPrice;
import com.thermofisher.dsc.amanda.common.excel.type.ParseExcelType;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.function.Consumer;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @author wenjie.yang on 4/3/2019.
 */
@Component
@Slf4j
public class ScheduleJob {

  public static final int totle = 150;

  @Autowired
  private ExcelCacheMapper customerCacheMapper;


//  @Scheduled(cron = "0 22 14 * * ? ")
//  public void twoSecondExport(){
//    log.info("==============ssssssss================");
//    inportExcelToDatabase(ParseExcelType.E1CustomerType);
//    log.info("==============ssssssss================");
//  }
//
//  @Scheduled(cron = "0 22 14 * * ? ")
//  public void fourSecondExport(){
//    inportExcelToDatabase(ParseExcelType.ITSCARCacheType);
//  }



  public void inportExcelToDatabase(ParseExcelType excelType){
    InputStream inputStream = null;
    ExcelListener listener = null;
    try {
      inputStream = new FileInputStream(new File(excelType.getSourcePath()));
//      inputStream = ShareFileTest.smbGet1("E1Customer.xlsx");
//      BufferedInputStream inputStream = new BufferedInputStream(inputStream);
      // 解析每行结果在listener中处理
      listener = new ExcelListener(excelType.getType());
      ExcelReader excelReader = new ExcelReader(inputStream,  null, listener);

      if(excelType.getType() == SkuPrice.class){
        //use map insert
        excelReader.read(new Sheet(excelType.getSheetNo(), excelType.getHeadLine(), null));
      }else{
        //use javaBean insert
        excelReader.read(new Sheet(excelType.getSheetNo(), excelType.getHeadLine(), excelType.getType()));
      }
      List datas = listener.getDatas();
      switch (excelType){
        case E1CustomerType:
          this.insertList(datas,s->customerCacheMapper.insertEoneCustomerCacheList(s));
          customerCacheMapper.refreshTable("eone_customer","eone_customer_cache","st_no,bt_no,email");
          break;
        case SaleCustomerType:
          this.insertList(datas,s->customerCacheMapper.insertSaleCustomerCacheList(s));
          customerCacheMapper.refreshTable("sales_customer","sales_customer_cache","st_no");
          break;
        case ITSCARCacheType:
          this.insertList(datas,s->customerCacheMapper.insertITSCARCacheList(s));
          customerCacheMapper.refreshTable("itsc_ar","itsc_ar_cache","bt_no");
          break;
        case InventoryExcelType:
          this.insertList(datas,s->customerCacheMapper.insertIventoryCacheList(s));
          customerCacheMapper.refreshTable("inventory","inventory_cache","storage_loc");
          break;
        case SkuPriceType:
          this.insertList(datas,s->customerCacheMapper.insertSkuPriceCacheList(s));
          customerCacheMapper.refreshTable("sku_price","sku_price_cache","sku_nbr");
          break;
        default:
          break;
      }
    } catch (Exception e) {
      log.error(e.toString());
    } finally {
      listener.clear();
      try {
        inputStream.close();
      } catch (IOException e) {
        e.printStackTrace();
      }
    }
  }

  private void insertList(List datas,Consumer<List> con){
    for (int i= 0;i<=datas.size()/totle;i++){
      if (i==datas.size()/totle){
        con.accept(datas.subList(i*totle,datas.size()));
      }else {
        con.accept(datas.subList(i*totle,(i+1)*totle));
      }
    }
  }



}
